#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
#define maxn 1505
#define inf 0x1fffffff
int n, m, u, v, w;
struct node
{
    int to, len;
    node(int to, int len) : to(to), len(len){};
};
vector<vector<node>> edge;
queue<int> que;
int dis[maxn];
int main()
{
    cin >> n >> m;
    edge.resize(n + 1);
    fill(dis, dis + n + 1, -1);
    for (int i = 0, u, v, w; i < m; ++i)
    {
        cin >> u >> v >> w;
        edge[u].push_back(node(v, w));
    }
    que.push(1);
    dis[1] = 0;
    while (!que.empty())
    {
        u = que.front();
        que.pop();
        for (int i = 0, to, len; i < edge[u].size(); ++i)
        {
            to = edge[u][i].to;
            len = edge[u][i].len;
            if (dis[to] < dis[u] + len)
            {
                que.push(to);
                dis[to] = dis[u] + len;
            }
        }
    }
    cout << dis[n] << endl;
    return 0;
}